package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("UnitDAO")
@Transactional
public class UnitDAOImpl extends AbstractJpaDao<Unit> implements UnitDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Unit.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public UnitDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Unit findUnitById(Integer id) throws DataAccessException {

		return findUnitById(id, -1, -1);
	}

	@Transactional
	public Unit findUnitById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUnitById", startResult, maxRows, id);
			return (Unit) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Unit> findUnitByName(String name) throws DataAccessException {

		return findUnitByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findUnitByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUnitByName", startResult, maxRows, name);
		return new LinkedHashSet<Unit>(query.getResultList());
	}

	@Transactional
	public Set<Unit> findUnitByNameContaining(String name) throws DataAccessException {

		return findUnitByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findUnitByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUnitByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Unit>(query.getResultList());
	}

	@Transactional
	public Set<Unit> findUnitByType(String type) throws DataAccessException {

		return findUnitByType(type, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findUnitByType(String type, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUnitByType", startResult, maxRows, type);
		return new LinkedHashSet<Unit>(query.getResultList());
	}

	@Transactional
	public Set<Unit> findUnitByTypeContaining(String type) throws DataAccessException {

		return findUnitByTypeContaining(type, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findUnitByTypeContaining(String type, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUnitByTypeContaining", startResult, maxRows, type);
		return new LinkedHashSet<Unit>(query.getResultList());
	}

	@Transactional
	public Set<Unit> findUnitByDescription(String description) throws DataAccessException {

		return findUnitByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findUnitByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findUnitByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Unit>(query.getResultList());
	}

	@Transactional
	public Set<Unit> findUnitByDescriptionContaining(String description) throws DataAccessException {

		return findUnitByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findUnitByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findUnitByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Unit>(query.getResultList());
	}

	@Transactional
	public Unit findUnitByPrimaryKey(Integer id) throws DataAccessException {
		return findUnitByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Unit findUnitByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUnitByPrimaryKey", startResult, maxRows, id);
			return (Unit) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Unit> findUnitsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUnitsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Unit> findAllUnits() throws DataAccessException {

		return findAllUnits(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findAllUnits(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllUnits", startResult, maxRows);
		return new LinkedHashSet<Unit>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Unit> findUnitByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Unit>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Unit> findUnitsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Unit.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Unit> units = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return units;
	}

	@Transactional
	public Unit findUnitByPhysicalPrimaryKey(String name, String type) throws DataAccessException {
		return findUnitByPhysicalPrimaryKey(name, type, -1, -1);
	}

	@Transactional
	public Unit findUnitByPhysicalPrimaryKey(String name, String type, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findUnitByPhyscialPrimaryKey", startResult, maxRows, name, type);
			return (Unit) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Unit entity) {
		return true;
	}
}
